library InsertShell initializer init requires LegacySystem, AmmoAPI

	private function InsertShellConditions takes nothing returns boolean
		return GetSpellAbilityId()=='A08L'
	endfunction

	private function InsertShell takes nothing returns nothing
		local unit u = GetTriggerUnit()
		local player p = GetOwningPlayer(u)
		local integer i = GetConvertedPlayerId(p)
		local integer current = udg_Second
		call TriggerSleepAction(0.)
		
		if udg_Reloading[i]==0 and GetPrimaryAmmoMagazines(p)>1 and GetPrimaryAmmoRounds(p)<18 then
			call AddPrimaryAmmo(u, 0, 1)
			set udg_InsertShells[i] = udg_InsertShells[i] + 1
			call SetPlayerState(p,PLAYER_STATE_RESOURCE_GOLD, GetPrimaryAmmoRounds(p))
			if current-udg_InsertShellsLimit!=0 then
				call AttachSoundToUnit(gg_snd_InsertShell,u)
				call SetSoundVolume(gg_snd_InsertShell,110)
				call StartSound(gg_snd_InsertShell)
			else
				set udg_InsertShellsLimit=current
			endif
			if udg_InsertShells[i]>=18 then
				call AddPrimaryAmmo(u, -1,0)
				set udg_InsertShells[i] = 0
			endif
		endif
		set u = null
	endfunction

	private function init takes nothing returns nothing
		local trigger t
		set t = CreateTrigger()
		call TriggerRegisterAnyUnitEventBJ(t,EVENT_PLAYER_UNIT_SPELL_EFFECT)
		call TriggerAddCondition(t, Condition(function InsertShellConditions) )
		call TriggerAddAction(t,function InsertShell)
	endfunction




endlibrary